//
//  SCStatusesListViewModel.swift
//  weibo
//
//  Created by 阿城 on 15/12/9.
//  Copyright © 2015年 阿城. All rights reserved.
//

import UIKit

class SCStatusesListViewModel: NSObject {
    
    var statusListArr:[SCStatusViewModel]?
    
    func loadData(pulling:Bool = false, backClosure: (isSeccess: Bool, number: Int)->()){
        
        var sinceId: Int64 = 0
        var maxId: Int64 = 0
        
        if pulling {
            maxId = (statusListArr?.last?.statusMod?.id ?? 1) - 1
        }else{
            sinceId = statusListArr?.first?.statusMod?.id ?? 0
        }
        
        SCStatusDAL.loadData(sinceId, maxId: maxId) { (result) -> () in
            
            guard let result = result else{
                backClosure(isSeccess: false, number: 0)
                return
            }
            
            //可变数组
            var statusList = [SCStatusViewModel]()
            
            for value in result {
                let statusMod = SCStatusModel(dict: value)
                let status = SCStatusViewModel()
                status.statusMod = statusMod
                statusList.append(status)
                //                printLog(status.statusMod?.id)
            }
            
            if self.statusListArr == nil{
                self.statusListArr = statusList
            }else if pulling {
                self.statusListArr = self.statusListArr! + statusList
            }else{
                self.statusListArr = statusList + self.statusListArr!
            }
            backClosure(isSeccess: true,number: statusList.count)
            
        }
        
        
        
    }
    
}
